import 'dart:convert';

import '../log/log.dart';
import 'i_store.dart';

// // 条件导入：移动平台使用MMKV，桌面/web使用shared_preferences
// // 条件导入逻辑优化
// import 'sp_store_create.dart'
// if (Platform.isAndroid) 'mmkv_store_create.dart'
// if (Platform.isIOS) 'mmkv_store_create.dart'
// if (dart.library.html) 'sp_store_create.dart'
// as platform_store;

extension CUStringExtends on String {
  bool putStore(dynamic value, {String? name}) => StoreUtils.setAny(this, value, name: name);

  String getStore({String def = "", String? name}) => StoreUtils.getString(this, def: def, name: name);

  String? getStoreOrNull({String def = "", String? name}) => StoreUtils.getStringOrNull(this, def: def, name: name);

  dynamic getStoreFromJson({String def = "", String? name}) {
    try {
      final jsonString = getStore(def: def, name: name);
      if (jsonString.isEmpty) {
        return null;
      }
      return jsonDecode(jsonString);
    } catch (e, s) {
      LogUtils.e("getStoreFromJson error", error: e, stackTrace: s);
      return null;
    }
  }

  int getIntStore({int def = 0, String? name}) => StoreUtils.getInt(this, def: def, name: name);

  int? getIntStoreOrNull({int def = 0, String? name}) => StoreUtils.getIntOrNull(this, def: def, name: name);

  bool getBoolStore({bool def = false, String? name}) => StoreUtils.getBool(this, def: def, name: name);

  bool? getBoolStoreOrNull({bool def = false, String? name}) => StoreUtils.getBoolOrNull(this, def: def, name: name);

  double getDoubleStore({double def = 0, String? name}) => StoreUtils.getDouble(this, def: def, name: name);

  double? getDoubleStoreOrNull({double def = 0, String? name}) => StoreUtils.getDoubleOrNull(this, def: def, name: name);

  bool storeRemove({String? name}) => StoreUtils.remove(this, name: name);

  bool storeContains({String? name}) => StoreUtils.contains(this, name: name);
}

class StoreUtils {
  StoreUtils._();

  static late IStore _store;

  /// 初始化
  static Future<void> init(IStore store) async {
    // if (AppUtils.isAndroid || AppUtils.isIOS) {
    //   _store = MMKVStore();
    // } else {
    //   _store = SpStore();
    // }
    // _store = platform_store.createStoreInstance();
    _store = store;
    LogUtils.i("当前StoreUtils 使用的协议是${_store.runtimeType}");
    await _store.init();
  }

  /// 获取字符串
  static String getString(String key, {String def = "", String? name}) {
    return _store.getString(key, def: def, name: name);
  }

  static String? getStringOrNull(String key, {String def = "", String? name}) {
    if (_store.contains(key, name: name)) {
      return getString(key, def: def, name: name);
    } else {
      return null;
    }
  }

  /// 设置字符串
  static bool setString(String key, String value, {String? name}) {
    return _store.setString(key, value, name: name);
  }

  /// 获取整数
  static int getInt(String key, {int def = 0, String? name}) {
    return _store.getInt(key, def: def, name: name);
  }

  static int? getIntOrNull(String key, {int def = 0, String? name}) {
    if (_store.contains(key, name: name)) {
      return getInt(key, def: def, name: name);
    } else {
      return null;
    }
  }

  /// 设置整数
  static bool setInt(String key, int value, {String? name}) {
    return _store.setInt(key, value, name: name);
  }

  /// 获取浮点数
  static double getDouble(String key, {double def = 0.0, String? name}) {
    return _store.getDouble(key, def: def, name: name);
  }

  static double? getDoubleOrNull(String key, {double def = 0.0, String? name}) {
    if (_store.contains(key, name: name)) {
      return getDouble(key, def: def, name: name);
    } else {
      return null;
    }
  }

  /// 设置浮点数
  static bool setDouble(String key, double value, {String? name}) {
    return _store.setDouble(key, value, name: name);
  }

  /// 获取布尔值
  static bool getBool(String key, {bool def = false, String? name}) {
    return _store.getBool(key, def: def, name: name);
  }

  static bool? getBoolOrNull(String key, {bool def = false, String? name}) {
    if (_store.contains(key, name: name)) {
      return getBool(key, def: def, name: name);
    } else {
      return null;
    }
  }

  /// 设置布尔值
  static bool setBool(String key, bool value, {String? name}) {
    return _store.setBool(key, value, name: name);
  }

  /// 获取字符串列表
  static List<String> getStringList(String key, {List<String> def = const [], String? name}) {
    return _store.getStringList(key, def: def, name: name);
  }

  static List<String>? getStringListOrNull(String key, {List<String> def = const [], String? name}) {
    if (_store.contains(key, name: name)) {
      return getStringList(key, def: def, name: name);
    } else {
      return null;
    }
  }

  /// 设置字符串列表
  static bool setStringList(String key, List<String> value, {String? name}) {
    return _store.setStringList(key, value, name: name);
  }

  /// 删除键值对
  static bool remove(String key, {String? name}) {
    return _store.remove(key, name: name);
  }

  /// 清空所有数据
  static void clear({String? name}) {
    _store.clear(name: name);
  }

  /// 判断是否存在键值对
  static bool contains(String key, {String? name}) {
    return _store.contains(key, name: name);
  }

  /// 批量设置键值对
  static bool setAny(String key, dynamic value, {String? name}) {
    try {
      if (value is String) {
        return setString(key, value, name: name);
      } else if (value is int) {
        return setInt(key, value, name: name);
      } else if (value is double) {
        return setDouble(key, value, name: name);
      } else if (value is bool) {
        return setBool(key, value, name: name);
      } else if (value is List<String>) {
        return setStringList(key, value, name: name);
      } else if (value is Map) {
        // 将 Map 转换为 JSON 字符串再存储
        final jsonString = jsonEncode(value);
        return setString(key, jsonString, name: name);
      } else {
        // 对于所有其他类型，尝试将它们转换为 JSON 字符串
        try {
          final jsonString = jsonEncode(value);
          return setString(key, jsonString, name: name);
        } catch (e) {
          throw UnsupportedError('Failed to convert to JSON: ${value.runtimeType}');
        }
      }
    } catch (e) {
      throw UnsupportedError('Failed to convert to JSON: ${value.runtimeType},$e');
    }
  }
}
